package com.hrt.demo.fannie.controllor;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.demo.fannie.bean.Classification;
import com.hrt.demo.fannie.bean.Products;
import com.hrt.demo.fannie.service.ClassificationService;
import com.hrt.demo.fannie.service.ProductsService;

@Controller
@RequestMapping("manage/fannie/products")
public class ProductsControllor extends BaseController<Products> {
	
	@Autowired
	private ProductsService productsService;
	
	@Autowired
	private ClassificationService classifService;
	private List<Classification> classifications;
	
	public List<Classification> getClassifications() {
		if(classifications==null){
			classifications = classifService.searchAll();
		}
		return classifications;
	}
	public void setClassifications(List<Classification> classifications) {
		this.classifications = classifications;
	}

	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(productsService.totalCount());

		List<Products> resultList = productsService.queryByPage(getPageIndex(),
				getPageSize());
		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}

	/**
	 * 查看
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();

		list = productsService.queryByPage(getPageIndex(), getPageSize());
		setTotalCount(productsService.totalCount());
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		classifications = classifService.searchAll();
		view.addObject("pageResult", page);
		view.addObject("classifications", classifications);
		return view;

	}

	/**
	 * 删除
	 * */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
			}
		}
		try {
			productsService.deleteById(idArr);

			return idArr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
	

	/**
	 * 添加
	 * */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(@ModelAttribute("products") Products products, Long classId,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			Classification classification = classifService.getById(classId);
			products.setClassification(classification);
			productsService.addProducts(products);

			response.sendRedirect("list.do");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 跳转到修改页面
	 * */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(@RequestParam(value = "id", required = true) String productsid,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (productsid != null && !"".equals(productsid)) {
			Products products = this.productsService.getById(new Long(productsid));
			byte[] buf = products.getPic();
			try {
				String strBuf = new String(buf,"UTF-8");
				products.setPic_str(strBuf);
				if (products != null) {
					map = transBean2Map(products);
				}
				
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

	/**
	 * 修改
	 * */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("products") Products products,Long classId,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			Classification classification = classifService.getById(classId);
			products.setClassification(classification);
			productsService.updateProducts(products);
			response.sendRedirect("list.do");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
